package com.xncoding.Queue;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {
    public static void main(String[] args) {
            BlockingQueue<String> queue = new SynchronousQueue<String>();

        new Thread("生产者") {
            public void run() {
                while (true) {
                    String data = UUID.randomUUID().toString();
                    try {
                        System.out.println("生产者 put: " + data);
                        queue.put(data);
                        Thread.sleep(5000);// 可修改时间测试
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();

        new Thread("消费者") {
            public void run() {
                while (true) {
                    try {
                        String data = queue.take();
                        System.out.println("消费者 take: " + data);
                        Thread.sleep(1000);// 可修改时间测试
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            };
        }.start();
    }
}
